package com.stone.common.action;


/**
 * <pre>
 * 高级查询
 * @author jz
 * </pre>
 */
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import net.sf.json.JSON;
import net.sf.json.JSONSerializer;
import net.sf.json.JsonConfig;

import com.stone.common.pojo.AdvanceSearch;
import com.stone.common.service.AdvanceSearchService;
import com.stone.utils.DateFormatTool;
import com.stone.utils.FileUtils;
import com.stone.utils.JsonUtils;
import com.stone.utils.Resources;
import com.stone.utils.StringUtils;

import frm.ssh.action.BaseAction;
import frm.ssh.exception.AppException;

public class AdvanceSearchAction extends BaseAction {
	
	private static final long serialVersionUID = -4647594073681054487L;

	private AdvanceSearchService advanceSearchService;
	
	private AdvanceSearch advanceSearch;
	
	private HashMap<String,String> conMap;
	
	private String frmConditionHtml;
	
	private String frmConditionWhere;
	
	private AdvanceSearch entity;
	
	private String id;
	
	/**
	 * 定制的条件字段
	 * @return
	 * @throws AppException
	 * @throws IOException 
	 */
	public void getCol() throws Exception {
		
		String condition = "";
		
		if (!StringUtils.isNullOrBlank(conMap.get("modelId"))) {
			condition += " a.modelId = '" + conMap.get("modelId") + "'"; 
		}
			
//		if (!StringUtils.isNullOrBlank(conMap.get("tableState"))) {
//			
//			if (!StringUtils.isNullOrBlank(condition)) {
//				condition += " and ";
//			}
//			condition += " a.tableState = '" + conMap.get("tableState") + "'"; 
//		}	
		
		// 查询form表单对应所有的字段
		List<AdvanceSearch>list = advanceSearchService.findListByHql(" from AdvanceSearch a where " + condition + " order by a.id");
		
        JsonConfig jsonConfig = JsonUtils.configJson(new String[] {}, "yyyy-MM-dd");
    	
        JSON json = JSONSerializer.toJSON(list, jsonConfig);
        
		
		super.getResponse().getWriter().print(json.toString());

		
	}
	
	/**
	 * 保存查询条件，存到硬盘，对应文件名存入数据库
	 * @return
	 * @throws AppException
	 * @throws IOException 
	 */
	public String saveCondition() throws Exception {
		
		// 取得路径
		String path = Resources.getString("advancesearch", "struts");
		FileUtils.createFolder(path);
		
		// 将查询条件HTML存入文件中
		String fileName = DateFormatTool.format(new Date(),true).replace(" ", "").replaceAll("-", "").replaceAll(":", "") + ".txt";
		File file = new File (path + fileName); 
		FileOutputStream out = new FileOutputStream(file); 
		out.write(this.frmConditionHtml.getBytes());
		out.close();
		
		// 查询条件拼的SQL
		file = new File (path + fileName.replace(".txt", "where.txt")); 
		out = new FileOutputStream(file); 
		out.write(frmConditionWhere.getBytes());
		out.close();
		
/*
		List<AdvanceSearchCondition> list = advanceSearchService.findListByHql(" from AdvanceSearchCondition where menuId = '" +  super.getSession().getAttribute(IConstant.SESSION_MENU_ID) + "' and userId = '" + ((TUser) super.getSession().getAttribute(IConstant.SESSION_TUSER)).getId() + "'");
		
		// 新建
		if (list.size() == 0) {
			AdvanceSearchCondition  c = new AdvanceSearchCondition();
			c.setConditionName(fileName);
			c.setMenuId((Long) super.getSession().getAttribute(IConstant.SESSION_MENU_ID));
			c.setUserId(((TUser) super.getSession().getAttribute(IConstant.SESSION_TUSER)).getId());
			c.setId(SeqUtil.getNextKey("SEQ_SYS"));
			advanceSearchService.save(c);
			
			// 修改
		} else {
			
			try {
				File fileInnerHtml = new File (path + list.get(0).getConditionName());
				fileInnerHtml.delete();
			} catch (Exception e) {
				
			}
			
			try {
				File fileWhere = new File (path + list.get(0).getConditionName().replace(".txt", "where.txt"));
				fileWhere.delete();
			} catch (Exception e) {
				
			}
			advanceSearchService.updateByHQL("update AdvanceSearchCondition set conditionName = ? where menuId = ? and userId = ?", 
					new Object[] {fileName,(Long) super.getSession().getAttribute(IConstant.SESSION_MENU_ID),((TUser) super.getSession().getAttribute(IConstant.SESSION_TUSER)).getId()});
			
		}*/
		
		return SUCCESS;
	}
	
	/**
	 * 进入详细页面
	 * @return
	 * @throws AppException
	 */
	public String initDetail() throws Exception {
		
		// 修改进入
		if (!"0".equals(id)) {
			
			this.entity = (AdvanceSearch)advanceSearchService.findById(Long.parseLong(id), AdvanceSearch.class.getName());
	
			// 新建进入
		} else {
			
			this.entity = new AdvanceSearch();
			this.entity.setId(Long.parseLong("0"));
			
		}
		
		return "CON_SUCCESS";
	}
	
	/**
	 * 保存
	 * @return
	 * @throws AppException
	 */
	public String save() throws AppException {
		
		// 新建时
		if (entity.getId() == 0) {

			advanceSearchService.save(entity);

			
		} else {
			advanceSearchService.update(entity);
		}
		
		super.setInfo("保存成功");
		
		return "CON_SUCCESS";
	}
	
	/**
	 * 删除
	 * @return
	 * @throws AppException
	 */
	public String delete() throws AppException {
		
		advanceSearchService.doDelAll(id, "AdvanceSearch");
		
		return "DEL-SUCCESS";
		
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public AdvanceSearchService getAdvanceSearchService() {
		return advanceSearchService;
	}

	public void setAdvanceSearchService(AdvanceSearchService advanceSearchService) {
		this.advanceSearchService = advanceSearchService;
	}

	public AdvanceSearch getAdvanceSearch() {
		return advanceSearch;
	}

	public void setAdvanceSearch(AdvanceSearch advanceSearch) {
		this.advanceSearch = advanceSearch;
	}

	public HashMap<String,String> getConMap() {
		return conMap;
	}

	public void setConMap(HashMap<String,String> conMap) {
		this.conMap = conMap;
	}

	public String getFrmConditionHtml() {
		return frmConditionHtml;
	}

	public void setFrmConditionHtml(String frmConditionHtml) {
		this.frmConditionHtml = frmConditionHtml;
	}

	public String getFrmConditionWhere() {
		return frmConditionWhere;
	}

	public void setFrmConditionWhere(String frmConditionWhere) {
		this.frmConditionWhere = frmConditionWhere;
	}

	public AdvanceSearch getEntity() {
		return entity;
	}

	public void setEntity(AdvanceSearch entity) {
		this.entity = entity;
	}
	
	

}
